﻿using System.Domain.Entity;
using System.Domain.IRepository;
using System.Linq;
using System.Repository.Base;
using System.Repository.DataBase.Models;
using Microsoft.EntityFrameworkCore;

namespace System.Repository
{
    public class SysModuleRepository : RepositoryDomain<SysModuleEntity, Sys_Module>, ISysModuleRepository
    {
        public SysModuleRepository(IDbContextConfig config) : base(config)
        {
        }

        public override bool Update(SysModuleEntity entity)
        {
            Sys_Module model = GetModel(entity.Id);
            if (model == null)
                return false;
            Dbcontext.Set<Sys_Module>().Attach(model);
            Dbcontext.Entry(model).State = EntityState.Unchanged;
            AutoCopy(model, entity);
            return Dbcontext.SaveChanges() > 0;
        }
        public override bool Delete(string key)
        {

            // 事务
            var temp = Transaction(() =>
            {
                //删除 模块权限
                var entitys2 = Dbcontext.Set<Sys_ModuleAuthorize>().Where(t => t.ModuleId == key).ToList();
                entitys2.ForEach(m => Dbcontext.Entry(m).State = EntityState.Deleted);
                //删除 角色授权
                var entitys1 = Dbcontext.Set<Sys_RoleAuthorize>().Where(t => t.ModuleId == key).ToList();
                entitys1.ForEach(m => Dbcontext.Entry(m).State = EntityState.Deleted);
                //删除 模块按钮
                var entitys = Dbcontext.Set<Sys_Module>().Where(t => t.Id == key).ToList();
                entitys.ForEach(m => Dbcontext.Entry(m).State = EntityState.Deleted);
                return Dbcontext.SaveChanges() > 0;

            });


            return temp;
        }


    }
}
